public class ThreadDemo1 {
    private static boolean flg = true;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                while(flg){
                    System.out.println("线程正在运行...");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("线程运行结束!");
            }
        };
        t.start();

        Thread.sleep(3000);

        flg = false;

    }
}
